package com.yanqu.road.dao.impl.channelcenter.washe;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.activity.washe.config.WasheRankConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WasheRankConfigDaoImpl extends TempDao {

    private static String getWasheRankConfigSql = "select * from `t_n_washepvp_rank` where `activityId`=? and ( `seasonId`=? or `seasonId`=? )";

    public Map<Integer, WasheRankConfig> getWasheRankConfigMap(int activityId, int seasonId) {
        Map<Integer, WasheRankConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelCenterDb());
            call = getDb().getConnection().prepareCall(getWasheRankConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, WasheConfig.SEASON_ID_NULL);
            call.setInt(i++, seasonId);
            rs = call.executeQuery();
            while (rs.next()) {
                WasheRankConfig washeRankConfig = new WasheRankConfig();
                washeRankConfig.setActivityId(rs.getInt("activityId"));
                washeRankConfig.setSeasonId(rs.getInt("seasonId"));
                washeRankConfig.setRankId(rs.getInt("rankId"));
                washeRankConfig.setRankScore(rs.getLong("rankScore"));
                washeRankConfig.setMatchRange(rs.getString("matchRange"));
                washeRankConfig.setWinPoint(rs.getLong("winPoint"));
                washeRankConfig.setLosePoint(rs.getLong("losePoint"));
                washeRankConfig.setEndReset(rs.getLong("endReset"));
                washeRankConfig.setShowSkin(rs.getString("showSkin"));
                washeRankConfig.setMedal(rs.getInt("medal"));

                // init
                washeRankConfig.setMatchRangeList(StringUtils.stringToIntegerList(washeRankConfig.getMatchRange(), "\\|"));

                map.put(washeRankConfig.getRankId(), washeRankConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
